package chapter03.d05;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/***
 * 创建一个大小无限的线程池。此线程池支持定时以及周期性执行任务的需求
 * @author heliteng
 *
 */
public class ScheduledThreadPollDemo {

	public static void main(String[] args) {
		// 创建一个大小无限的线程池。此线程池支持定时以及周期性执行任务的需求
		ExecutorService pool = Executors.newScheduledThreadPool(2);

		Runnable task1 = new ScheduledTask();
		Runnable task2 = new ScheduledTask();
		Runnable task3 = new ScheduledTask();

		pool.execute(task3);
		pool.execute(task2);
		pool.execute(task1);
		pool.execute(new ScheduledTask());
		pool.execute(new ScheduledTask());
		pool.execute(new ScheduledTask());

		// 等待已提交的任务全部结束 不再接受新的任务
		pool.shutdown();
	}
}

class ScheduledTask implements Runnable {

	@Override
	public void run() {
		System.out.println(Thread.currentThread().getName() + "正在执行… …");
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println(Thread.currentThread().getName() + "执行完毕");
	}
}
